import { useModel } from "umi";
import { useReactive } from "ahooks";

const useOrganizationDatabase = () => {
    const { remoteData } = useModel("global");

    const state = useReactive({
        dataLoading: false,
        databases: [],
        versionLoading: false,
        versions: [],
    });

    const loadDatabases = async () => {
        state.dataLoading = true;
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/resource/referenceDatabase/organization/getAll",
            params: {},
        });
        state.databases = res || [];
        state.dataLoading = false;
    };

    const loadDatabaseVersionsByDatabaseName = async (databaseName) => {
        state.versionLoading = true;
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/resource/referenceDatabase/organization/getVersionByDatabaseName",
            params: { databaseName },
        });
        state.versions = res || [];
        state.versionLoading = false;
    };

    return { ...state, loadDatabases, loadDatabaseVersionsByDatabaseName };
};

export default useOrganizationDatabase;
